class Tank {
    constructor(x, y, color) {
        this.x = x;
        this.y = y;
        this.color = color;
        this.width = 40;
        this.height = 40;
        this.speed = 5;
        this.direction = 'up';
        this.bullets = [];
    }

    draw(ctx) {
        ctx.save();
        ctx.fillStyle = this.color;
        ctx.translate(this.x, this.y);
        
        // 根据方向旋转坦克
        switch(this.direction) {
            case 'up':
                ctx.rotate(0);
                break;
            case 'right':
                ctx.rotate(Math.PI / 2);
                break;
            case 'down':
                ctx.rotate(Math.PI);
                break;
            case 'left':
                ctx.rotate(-Math.PI / 2);
                break;
        }

        // 绘制坦克主体
        ctx.fillRect(-this.width/2, -this.height/2, this.width, this.height);
        
        // 绘制炮管
        ctx.fillStyle = '#666';
        ctx.fillRect(-5, -this.height/2, 10, -20);
        
        ctx.restore();
    }

    move(direction) {
        this.direction = direction;
        switch(direction) {
            case 'up':
                this.y -= this.speed;
                break;
            case 'right':
                this.x += this.speed;
                break;
            case 'down':
                this.y += this.speed;
                break;
            case 'left':
                this.x -= this.speed;
                break;
        }
    }

    shoot() {
        const bullet = new Bullet(this.x, this.y, this.direction);
        this.bullets.push(bullet);
    }
}

class Bullet {
    constructor(x, y, direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.speed = 10;
        this.radius = 5;
    }

    draw(ctx) {
        ctx.fillStyle = '#ff0';
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
        ctx.fill();
    }

    update() {
        switch(this.direction) {
            case 'up':
                this.y -= this.speed;
                break;
            case 'right':
                this.x += this.speed;
                break;
            case 'down':
                this.y += this.speed;
                break;
            case 'left':
                this.x -= this.speed;
                break;
        }
    }
}

class Game {
    constructor() {
        this.canvas = document.getElementById('gameCanvas');
        this.ctx = this.canvas.getContext('2d');
        this.canvas.width = 800;
        this.canvas.height = 600;
        
        this.player = new Tank(this.canvas.width/2, this.canvas.height/2, '#0f0');
        this.enemies = [];
        this.score = 0;
        this.lives = 3;
        
        this.keys = {};
        this.setupEventListeners();
        this.spawnEnemies();
        this.gameLoop();
    }

    setupEventListeners() {
        window.addEventListener('keydown', (e) => {
            this.keys[e.key] = true;
            if (e.key === ' ') {
                this.player.shoot();
            }
        });

        window.addEventListener('keyup', (e) => {
            this.keys[e.key] = false;
        });
    }

    spawnEnemies() {
        setInterval(() => {
            if (this.enemies.length < 5) {
                const x = Math.random() * this.canvas.width;
                const y = Math.random() * this.canvas.height;
                this.enemies.push(new Tank(x, y, '#f00'));
            }
        }, 2000);
    }

    update() {
        // 更新玩家位置
        if (this.keys['ArrowUp']) this.player.move('up');
        if (this.keys['ArrowRight']) this.player.move('right');
        if (this.keys['ArrowDown']) this.player.move('down');
        if (this.keys['ArrowLeft']) this.player.move('left');

        // 更新子弹
        this.player.bullets.forEach((bullet, index) => {
            bullet.update();
            if (bullet.x < 0 || bullet.x > this.canvas.width || 
                bullet.y < 0 || bullet.y > this.canvas.height) {
                this.player.bullets.splice(index, 1);
            }
        });

        // 检测碰撞
        this.checkCollisions();
    }

    checkCollisions() {
        // 检测子弹与敌人的碰撞
        this.player.bullets.forEach((bullet, bulletIndex) => {
            this.enemies.forEach((enemy, enemyIndex) => {
                const dx = bullet.x - enemy.x;
                const dy = bullet.y - enemy.y;
                const distance = Math.sqrt(dx * dx + dy * dy);
                
                if (distance < enemy.width/2) {
                    this.player.bullets.splice(bulletIndex, 1);
                    this.enemies.splice(enemyIndex, 1);
                    this.score += 10;
                    document.getElementById('score').textContent = this.score;
                }
            });
        });
    }

    draw() {
        this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
        
        // 绘制玩家
        this.player.draw(this.ctx);
        
        // 绘制子弹
        this.player.bullets.forEach(bullet => bullet.draw(this.ctx));
        
        // 绘制敌人
        this.enemies.forEach(enemy => enemy.draw(this.ctx));
    }

    gameLoop() {
        this.update();
        this.draw();
        requestAnimationFrame(() => this.gameLoop());
    }
}

// 启动游戏
window.onload = () => {
    new Game();
}; 